package io.github.rascaler.dcenter.context;

import io.github.rascaler.dcenter.annotation.OuterResponseBody;
import io.github.rascaler.dcenter.message.BasicEcode;
import io.github.rascaler.dcenter.pojo.OuterResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.io.IOException;
import java.util.List;

public class ResponseBodyWrapHandler extends RequestResponseBodyMethodProcessor {
    public ResponseBodyWrapHandler(List<HttpMessageConverter<?>> messageConverters) {
        super(messageConverters);
    }

    public ResponseBodyWrapHandler(List<HttpMessageConverter<?>> converters, List<Object> requestResponseBodyAdvice) {
        super(converters, requestResponseBodyAdvice);
    }

    public boolean supportsReturnType(MethodParameter returnType) {
        return returnType.getMethodAnnotation(OuterResponseBody.class) != null;
    }

    public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException {
        OuterResult result = new OuterResult(BasicEcode.SUCCESS, "成功", returnValue);
        super.handleReturnValue(result, returnType, mavContainer, webRequest);

    }
}